<head>
    <link href="${ctx}/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
    <link href="${ctx}/font-awesome/3.0.0/css/font-awesome.min.css" rel="stylesheet">
    <link href="${ctx}/css/index.css" rel="stylesheet">
    <link href="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.css" rel="stylesheet">
    <script src="${ctx}/jquery/2.1.4/jquery.min.js"></script>
    <script src="${ctx}/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/bootstrap-table.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/locale/bootstrap-table-zh-CN.min.js"></script>
    <script src="${ctx}/bootstrap-table/1.12.0/extensions/export/bootstrap-table-export.min.js"></script>
    <script src="${ctx}/js/extends.js"></script>
    <script src="${ctx}/layui/layui.all.js"></script>
    <script src="${ctx}/js/util.js"></script>
    <link href="${ctx}/layui/css/layui.css" rel="stylesheet">
</head>
<title>分页</title>
<div class="lay-box">
    <form class="layui-form" action="" style="margin-top:10px">
        <div class="layui-inline" style="margin: 0 0 8px 20px;">
            <label class="layui-form-label" style="width: 95px">起始时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="star" placeholder="请选择时间">
            </div>
        </div>
        <div class="layui-inline" style="margin: 0 0 8px 20px;">
            <label class="layui-form-label" style="width: 95px">结束时间</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="end" placeholder="请选择时间">
            </div>
        </div>
        <div style="width: 150px;display: inline-block;margin: 0 0 8px 20px;">
            <select name="sdk_key" id="sdk_key" lay-verify="" style="width:300px">
                <option value="">选择数据源</option>
                <#list item as list>
                    <option value="${list.sdkKey}">${list.taskName}</option>
                </#list>

            </select>
        </div>
        <span onclick="search()" class="btn btn-info" style="height: 38px;margin-left: 10px;font-size: 14px">查询</span>
    </form>

    <table class="table table-border table-bordered table-hover table-bg table-sort" id="layTable"
           lay-filter="tableFilter">

    </table>
</div>

<style>
    .lay-box {
        padding: 60px 12px;
    }

    .layui-form-checkbox[lay-skin=primary] i {
        top: 5px;
    }
</style>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="save" >查看明细</a>
<#--    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>-->
</script>


<!--请在下方写此页面业务相关的脚本-->
<script type="text/html" id="switchTpl">
    <!-- 这里的 checked 的状态只是演示 -->
    <input type="checkbox" name="sex" value="{{d.id}}" lay-skin="switch" lay-text="启用|禁用" lay-filter="sexDemo" {{
           d.enable== 1 ? 'checked' : '' }}>
</script>

<script type="text/javascript">
    layui.use('laydate', function () {
        var laydate = layui.laydate;
        laydate.render({
            elem: '#star'
            , type: 'datetime'

        });
        laydate.render({
            elem: '#end'
            , type: 'datetime'

        });
    });
    // layui.use('laydate', function () {
    //     var laydate = layui.laydate;
    //     laydate.render({
    //         elem: '#star'
    //         , type: 'datetime'
    //         , format: 'yyyyMMdd'
    //
    //     });
    //     laydate.render({
    //         elem: '#end'
    //         , type: 'datetime'
    //         , format: 'yyyyMMdd'
    //     });
    //     laydate.render({
    //         elem: '#last'
    //         , type: 'date'
    //         ,format: 'yyyy-MM-dd'
    //     });
    // });
    var startDate = "";
    var endDate = "";
    var searchKey = "";
    var tableIns;
    var ids = "";
    var form;
    var table = "cences_req_ad_count";
    layui.use('table', function () {
        tableIns = layui.table,
            form = layui.form;
        form.render();
        tableIns.render({
            elem: '#layTable'
            ,
            url: '/sys/cencesreqadcount/list'
            ,
            method: 'POST'
            ,
            where: {
                status: 0,
            }
            ,
            id: 'dataTableId'
            ,
            cols: [[
                {type: 'checkbox', field: 'id'},
                {
                    field: 'createTime',
                    title: '创建时间',
                    width: 150,
                    templet:function(data){
                        return new Date(data.createTime).format("yyyy-MM-dd HH:mm:ss");
                    },
                    align: 'center',
                    sort: true
                }
                , {
                    field: 'reqCount',
                    title: '请求成功数',
                    width: 150,
                    align: 'center',
                    sort: true
                }
                , {
                    field: 'memberId',
                    title: '用户id',
                    width: 150,
                    align: 'center',
                    sort: true
                }
                , {
                    field: 'sdkKey',
                    title: '数据源',
                    width: 150,
                    align: 'center',
                    sort: true
                }
                , {
                    field: 'adId',
                    title: '广告位id',
                    width: 150,
                    align: 'center',
                    sort: true
                }
                , {fixed: 'right',
                    title: '操作',
                    toolbar: '#barDemo',
                    width: 150,
                    align: 'center',
                }

            ]]
            ,
            page: true
        });

        form.on('switch(sexDemo)', function (obj) {
            // layer.tips(this.value + ' ' + this.name + '：'+ obj.elem.checked, obj.othis);
            if (obj.elem.checked) {
                commonenable(this.value, 1);
            } else {
                commonenable(this.value, 0);
            }

        });
        tableIns.on('checkbox(tableFilter)', function (obj) {
            var checkStatus = tableIns.checkStatus('dataTableId');
            var dataList = checkStatus.data;
            if (dataList != null && dataList.length > 0) {

                for (var i = 0; i < dataList.length; i++) {
                    ids += dataList[i].id + ",";
                }
            }
        });

        function deleteId(ids) {
            if (ids != "") {
                $.ajax({
                    url: "/sys/cencesreqadcount/delete",
                    type: 'post',
                    data: { "id": ids},
                    async: false,
                    dataType: "json",
                    success: function (arg) {
                        if (arg) {
                            layer.msg(arg.message);
                            search();
                        } else {
                            layer.msg(arg.message);
                        }
                    }
                });
            }
        }

        $("#multiDelete").click(function () {
            deleteId(ids)
        });

        tableIns.on('tool(tableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'detail') {
                layer.msg('ID：' + data.id + ' 的查看操作');
            } else if (obj.event === 'banned') {
                layer.confirm('是否封禁', function (index) {
                    banned(data.memberId)
                    layer.close(index);
                });
            } else if (obj.event === 'save') {
                xbls(data.memberId);
            }
        });
    });

    function search() {
        var star = $("#star").val();
        var end = $("#end").val();
        var upstatus = $("#upStatus").val();
        var sdkKey = $("#sdk_key").val();
        tableIns.reload('dataTableId', {
            where: {
                'star': star, 'end': end,'status':upstatus,'sdkKey': sdkKey,
            }
            , page: {}
        });
    }



    function gradeEdit() {
        layer_show("新增", "/sys/cencesreqadcount/add", "650", "600");
    }

    function showmsg(msg) {
        layer.msg(msg)
    }

    function commonenable(id, status) {
        $.ajax({
            url: "/common/commonSwitch",
            type: 'post',
            data: {"table": table, "id": id, "status": status, "feild": "has_deleted"},
            async: false,
            dataType: "json",
            success: function (req) {
                if (req.success) {
                    search();
                    layer.msg(req.message, {icon: 1})
                } else {
                    layer.msg(req.message, {icon: 2})
                }
            }
        });
    }
    //封禁
    function banned(memberId) {
        $.ajax({
            url: "/sys/cencesreqadcount/banned",
            type: 'post',
            data: {"memberId": memberId},
            async: false,
            dataType: "json",
            success: function (req) {
                if (req.success) {
                    search();
                    layer.msg(req.message, {icon: 1})
                } else {
                    layer.msg(req.message, {icon: 2})
                }
            }
        });
    }
    // 查看明细
    var xbls = function (memberId) {
        var id = 'trade_list';
        var title = '收支明细';
        var href = '/sys/membernewtrade/list?memberId=' + memberId;
        if (id) {
            window.parent.iw.open({
                id: id,
                title: title,
                href: href
            });
        }
    };

    //批量操作
    function banAll() {
        var idsList = "";
        var checkStatus = tableIns.checkStatus('dataTableId');
        var dataList = checkStatus.data;
        var list = [];
        if (dataList != null && dataList.length > 0) {
            var item = new Object();
            for (var i = 0; i < dataList.length; i++) {
                item.memberId = dataList[i].memberId;
                list.push(item);
            }
        }
        let set = new Set(list);
        idsList = JSON.stringify(set);
        if(idsList===""){
            layer.msg("请选择要确认的记录")
            return;
        }

        layer.confirm('确认封禁么?', {icon: 3, title:'提示'}, function(index){
            //do something
            confirmBanList(idsList);
            layer.close(index);
        });



    }
    function confirmBanList(idlist) {
        //禁止登陆
        $.ajax({
            url: "/sys/pkgblacklog/banall",    //请求的url地址
            dataType: "json",   //返回格式为json
            async: true,//请求是否异步，默认为异步，这也是ajax重要特性
            data: {list: idlist},
            type: "post",   //请求方式
            beforeSend: function () {
                //请求前的处理
            },
            success: function (req) {
                //请求成功时处理

                alert(req.message)
            },
            complete: function () {
                //请求完成的处理
            },
            error: function () {
                //请求出错处理
                layer.msg("请重试！若在失败请联系管理员")
            }
        });

    }


</script>
<!--/请在上方写此页面业务相关的脚本-->
